package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.service.OrderService;
import com.markerhub.service.TicklistService;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
//@RequestMapping("/ticklist")
public class TicklistController {
    @Autowired
    TicklistService ticklistService;
    @Autowired
    OrderService OrderService;
    @Autowired
    HttpServletRequest request;


    @GetMapping("/tick/{tickid}")//搜的是ticklist表根据票务id进行票务日期遍历按钮
    public Result getTickid(@RequestParam(defaultValue = "1")  Integer currentpage , @PathVariable(name="tickid") String tickid){
        System.out.println("tickid:"+tickid);
        Page page = new Page(currentpage,5);//通过tick/1(tickid)的方式进行搜索之后会进行分页
        IPage pageData= ticklistService.page(page,new QueryWrapper<Ticklist>().eq("tick_id",tickid));
        return  Result.succ(pageData);
    }


    //简单购买
    @GetMapping("/tick/buy/{buytickid}/{userid}")
    public Result buyByTickid(@PathVariable(name="buytickid") String buytickid, @PathVariable(name="userid") int userid){//测试在线购买
        System.out.println("buytickid:"+buytickid);
        Ticklist ticklist=null;
        int ticki2int=Integer.parseInt(buytickid);
        QueryWrapper<Address> addressList=new QueryWrapper<Address>().eq("user_id", userid);
        System.out.println("个人地址查询测试");
        System.out.println(addressList.toString());
        Order order= new Order();//同时对两个表进行操作，
        if(buytickid!=null){
            ticklist=ticklistService.getById(buytickid);
            System.out.println(ticklist);
            ticklist.setTickNum(ticklist.getTickNum()-1);
            ticklist.setTickId(buytickid);

            ticklistService.saveOrUpdate(ticklist);
            //System.out.println("shiro"+ShiroUtil.getProfile().getId());
            User user=new User();

            System.out.println(request.getHeader("Authorization"));
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+SecurityUtils.getSubject().getSession().getAttribute("USER_SESSION"));
            System.out.println("111111111111111111111"+request.getSession().getAttribute("USER_SESSION"));
            order.setUserId(userid).setCreated(LocalDateTime.now()).setPrice(ticklist.getPrice()).setTicklistId(ticklist.getId()).setAddressId(1).setTickId(ticki2int);
                    //.setUserId(userid).setCreated(LocalDateTime.now()).setPrice(ticklist.getPrice());
            OrderService.saveOrUpdate(order);//对订单标进行创建订单同时结合用户
            System.out.println("ok"+order+"ok"+ticklist.getId());
            return Result.succ("修改成功");
        }else {
            return Result.faile("购买失败");
        }

    }






}
